package com.songlanyun.modules.distributionconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopConfig;
import com.songlanyun.modules.distributionconfig.model.DistributionShopGoodsConfigDTO;
import com.songlanyun.modules.distributionconfig.service.DistributionShopConfigService;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.distributionconfig.dao.DistributionShopGoodsConfigDao;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopGoodsConfig;
import com.songlanyun.modules.distributionconfig.service.DistributionShopGoodsConfigService;
import org.springframework.transaction.annotation.Transactional;


@Service("distributionShopGoodsConfigService")
public class DistributionShopGoodsConfigServiceImpl extends ServiceImpl<DistributionShopGoodsConfigDao, DistributionShopGoodsConfig> implements DistributionShopGoodsConfigService {

    @Autowired
    private DistributionShopConfigService distributionShopConfigService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public DistributionShopGoodsConfig create(DistributionShopGoodsConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public DistributionShopGoodsConfig modify(DistributionShopGoodsConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistributionShopGoodsConfig> page = this.page(
                new Query<DistributionShopGoodsConfig>().getPage(params),
                new QueryWrapper<DistributionShopGoodsConfig>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取商家商品级分销设置
     * @param shopId
     * @param goodsId
     * @return
     */
    @Override
    public DistributionShopGoodsConfig getShopAndGoods(Long shopId, Long goodsId) {
        return  this.getOne(new LambdaQueryWrapper<DistributionShopGoodsConfig>()
                .eq(DistributionShopGoodsConfig::getShopId, shopId)
                .eq(DistributionShopGoodsConfig::getGoodsId, goodsId)
                .last("limit 1"));
    }

    /**
     * 保存家商品级分销设置
     * @param shopId
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public DistributionShopGoodsConfig set(Long shopId, DistributionShopGoodsConfigDTO dto) {

        DistributionShopConfig shopConfig = distributionShopConfigService.getByShop(shopId);
        if(shopConfig==null)
        {
            throw new RRException("店铺级的一二级分佣还没有设置");
        }

        Goods goods = goodsService.getByShopIdAndId(shopId, dto.getGoodsId());
        DistributionShopGoodsConfig config = this.getShopAndGoods(shopId, dto.getGoodsId());
        if(config==null)
        {
            DistributionShopGoodsConfig distributionShopGoodsConfig = new DistributionShopGoodsConfig();
            distributionShopGoodsConfig.setShopId(shopId);
            distributionShopGoodsConfig.setGoodsId(goods.getId());
            distributionShopGoodsConfig.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            distributionShopGoodsConfig.setDefaultShop(dto.getDefaultShop());
            distributionShopGoodsConfig.setFirstLevelPrice(dto.getFirstLevelPrice());
            distributionShopGoodsConfig.setSecondLevelPrice(dto.getSecondLevelPrice());
            distributionShopGoodsConfig.setFirstLevelRatio(dto.getFirstLevelRatio());
            distributionShopGoodsConfig.setSecondLevelRatio(dto.getSecondLevelRatio());
            distributionShopGoodsConfig.setOpen(dto.getOpen());
            this.create(distributionShopGoodsConfig);
            return distributionShopGoodsConfig;
        }
        else
        {
            config.setComputeMode(GoodsConstant.ComputeMode.getByCode(dto.getComputeMode()));
            config.setDefaultShop(dto.getDefaultShop());
            config.setFirstLevelPrice(dto.getFirstLevelPrice());
            config.setSecondLevelPrice(dto.getSecondLevelPrice());
            config.setFirstLevelRatio(dto.getFirstLevelRatio());
            config.setSecondLevelRatio(dto.getSecondLevelRatio());
            config.setOpen(dto.getOpen());
            this.modify(config);
            return config;

        }
    }
}